package com.ruoyi.material.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.material.domain.SysUserMaterial;
import com.ruoyi.material.domain.UserData;
import com.ruoyi.material.domain.WeekData;
import com.ruoyi.material.mapper.SysUserMaterialMapper;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.SysMaterialMapper;
import com.ruoyi.material.domain.SysMaterial;
import com.ruoyi.material.service.ISysMaterialService;

/**
 * 上传材料Service业务层处理
 *
 * @author hsx
 * @date 2022-01-07
 */
@Service
public class SysMaterialServiceImpl implements ISysMaterialService
{
    @Autowired
    private SysMaterialMapper sysMaterialMapper;

    @Autowired
    private ISysRoleService iSysRoleService;

    @Autowired
    private SysUserMaterialMapper sysUserMaterialMapper;

    /**
     * 查询上传材料
     *
     * @param materialId 上传材料主键
     * @return 上传材料
     */
    @Override
    public SysMaterial selectSysMaterialByMaterialId(Long materialId)
    {
        return sysMaterialMapper.selectSysMaterialByMaterialId(materialId);
    }

    /**
     * 查询上传材料列表
     *
     * @param sysMaterial 上传材料
     * @return 上传材料
     */
    @Override
    public List<SysMaterial> selectSysMaterialList(SysMaterial sysMaterial)
    {
        //获取当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是不是admin
        if(user.isAdmin()){
            return sysMaterialMapper.selectSysMaterialList(sysMaterial);
        }
        sysMaterial.setDeptId(user.getDeptId());
        List<SysMaterial> sysMaterials = sysMaterialMapper.selectSysMaterialList(sysMaterial);
        // 判断是否显示结果
        if(!sysMaterial.getShowResult()){
            for (int i = 0; i < sysMaterials.size(); i++) {
                sysMaterials.get(i).setNumber(null);
                sysMaterials.get(i).setScore(null);
            }
        }
        return sysMaterials;
    }

    /**
     * 新增上传材料
     *
     * @param sysMaterial 上传材料
     * @return 结果
     */
    @Override
    public int insertSysMaterial(SysMaterial sysMaterial)
    {
        //获取当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是不是admin
        if(user.isAdmin()){
            return 0;
        }

        //设置属性
        sysMaterial.setDeptId(user.getDeptId());
        sysMaterial.setUploadBy(user.getNickName());
        sysMaterial.setUploadTime(new Date());

        int res = sysMaterialMapper.insertSysMaterial(sysMaterial);

        // 查询所有的评分专家
        List<Long> list = sysUserMaterialMapper.expertList("expert");
        if(!list.isEmpty()){
            // 为每个评分专家填加一条sys_user_material的关连
            sysUserMaterialMapper.insertSysUserMaterial(list,sysMaterial.getMaterialId());
        }

        return res;
    }

    /**
     * 修改上传材料
     *
     * @param sysMaterial 上传材料
     * @return 结果
     */
    @Override
    public int updateSysMaterial(SysMaterial sysMaterial)
    {
        return sysMaterialMapper.updateSysMaterial(sysMaterial);
    }

    /**
     * 批量删除上传材料
     *
     * @param materialIds 需要删除的上传材料主键
     * @return 结果
     */
    @Override
    public int deleteSysMaterialByMaterialIds(Long[] materialIds)
    {
        return sysMaterialMapper.deleteSysMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除上传材料信息
     *
     * @param materialId 上传材料主键
     * @return 结果
     */
    @Override
    public int deleteSysMaterialByMaterialId(Long materialId)
    {
        return sysMaterialMapper.deleteSysMaterialByMaterialId(materialId);
    }

    @Override
    public UserData getUserData() {
        UserData userData = new UserData();
        userData.setMaterialCount(sysMaterialMapper.getMaterialCount());
        List<Integer> userCount = sysMaterialMapper.getUserCount();
        userData.setAdminCount(userCount.get(0));
        userData.setDeptCount(userCount.get(1));
        userData.setExpertCount(userCount.get(2));
        return userData;
    }

    @Override
    public List<WeekData> getWeekData() {
        List<WeekData> weekData = sysMaterialMapper.getWeekData();
        return weekData;
    }
}
